// pages/applicantInfo/applicantInfo.js
import http from "../../utils/http";

Page({
  /**
   * 页面的初始数据
   */
  data: {
    applicantInfo: {},
    majorname: "",
    gradename: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const applicantInfo = JSON.parse(decodeURIComponent(options.applicantInfo));
    // 检查openid是否存在
    if (!applicantInfo.openid) {
      console.error('警告：传递的数据中缺少openid字段');
    }
    if (applicantInfo.major !== null) this.setMajorname(applicantInfo);
    if (applicantInfo.grade !== null) this.setGradename(applicantInfo);
    this.setData({
      applicantInfo,
    });
  },

  /**
   * 根据数据项中的major数值，将其转换成majorname
   */
  setMajorname: function (applicantInfo) {
    http
      .get(`/school/getMajorNameById/${applicantInfo.major}`)
      .then((res) => {
        if (res && res.data && res.data.majorName) {
          this.setData({
            majorname: res.data.majorName,
          });
        } else {
          wx.showToast({
            title: "获取专业名称失败",
            icon: "none",
          });
          console.log("API响应数据异常:", res);
        }
      })
      .catch((err) => {
        console.error("获取专业名称请求失败:", err);
        wx.showToast({
          title: "网络错误",
          icon: "none",
        });
      });
  },

  /**
   * 根据数据项中的grade数值，将其转换成gradename
   */
  setGradename: function (applicantInfo) {
    let gradename = "";
    switch (applicantInfo.grade) {
      case 1:
        gradename = "大一";
        break;
      case 2:
        gradename = "大二";
        break;
      case 3:
        gradename = "大三";
        break;
      case 4:
        gradename = "大四";
        break;
      case 5:
        gradename = "研究生";
        break;
      case 6:
        gradename = "博士";
        break;
      default:
        gradename = "----";
        console.error("grade name convertion error:\nunknown grade number");
        break;
    }
    this.setData({
      gradename,
    });
  },

  //参与项目
  joinProject(){
    // 传递申请人的openid到userJoinProject页面
    console.log('准备传递的申请人信息:', this.data.applicantInfo);
    const applicantOpenid = this.data.applicantInfo.openid || '';
    console.log('传递的openid:', applicantOpenid);
    
    if (!applicantOpenid) {
      wx.showToast({
        title: "用户信息不完整",
        icon: "none"
      });
      console.error('无法获取申请人openid，当前申请人信息:', this.data.applicantInfo);
      return;
    }
    
    wx.navigateTo({
      url: `../userJoinProject/userJoinProject?openid=${encodeURIComponent(applicantOpenid)}`,
    })
  },
});